/*   The MIT License
 *   
 *   Carina Engine
 *   Copyright (c) 2009 2010 2011 2012 Zdravko Velinov
 *   
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 */

%{
#include "carina/common/global.hh"
#include "carina/common/memory.hh"
#include "carina/common/logging.hh"
#include "carina/csp-driver.hh"
#include "carina/ast.hh"
#include "csp-parser.hh"

#undef yywrap
#define yywrap() 1

#define yyterminate() return Parser::token::T_END
%}

%option noyywrap nounput batch
%option never-interactive
%option prefix="csp_"

%{
#define YY_USER_ACTION  yylloc->columns(yyleng);

using namespace Carina::CSP;
using namespace Carina;
%}

IDENTIFIER  [_a-zA-Z][_a-zA-Z0-9]*

%x C_COMMENT
%x CPP_COMMENT

%%
%{
    yylloc->step();
%}

"variable"                              *yylval = AST::Node(); return Parser::token::T_VARIABLE;
"texture"                               *yylval = AST::Node(); return Parser::token::T_TEXTURE;
"program"                               *yylval = AST::Node(); return Parser::token::T_PROGRAM;
"material"                              *yylval = AST::Node(); return Parser::token::T_MATERIAL;
"profile"                               *yylval = AST::Node(); return Parser::token::T_PROFILE;
"common"                                *yylval = AST::Node(); return Parser::token::T_COMMON;
{IDENTIFIER}                            {
                                            *yylval = AST::CreateNode<AST::Value<string>>(*yylloc, yytext);
                                            return Parser::token::T_IDENTIFIER;
                                        }
\"(\\.|[^\\"])*\"                       {
                                            yytext[strlen(yytext)-1] = 0;
                                            *yylval = AST::CreateNode<StringLiteral>(*yylloc, yytext+1);
                                            return Parser::token::T_STRING_LITERAL;
                                        }
[,;=\{\}]                               *yylval = AST::Node(); return static_cast<Parser::token_type>(yytext[0]);

"//"                                    { BEGIN(CPP_COMMENT); }
<CPP_COMMENT>\n                         { BEGIN(INITIAL); yylloc->lines(yyleng); }
<CPP_COMMENT>.                          { }
"/*"                                    { BEGIN(C_COMMENT); }
<C_COMMENT>"*/"                         { BEGIN(INITIAL); }
<C_COMMENT>\n                           yylloc->lines(yyleng);
<C_COMMENT>.                            { }
[ \t]+                                  yylloc->step();
[\r\n]+                                 yylloc->lines(yyleng); yylloc->step();

.                                       driver.error(*yylloc, string("invalid character: ") + yytext);
%%

namespace Carina
{
namespace CSP
{
#include "ast-driver-parse.impl.cc"
}
}